﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql

''' <summary>
''' Interaction logic for ChangePasswordWindow.xaml
''' </summary>
Partial Public Class ChangePasswordWindow
    Inherits Window
#Region "These are global object which will be use to communicate with the database whithin this class"
    Private objSqlDatabase As New SqlDatabase(GlobalClass._ConStr)
    Private objDatabaseClass As New DatabaseClass(GlobalClass._ConStr)
#End Region

    Private _OldPassword As String = GlobalClass._Password
    Private _UserID As Int32 = GlobalClass._UserID

    Public Sub New()
        InitializeComponent()
        pbCurrentPassword.Focus()
    End Sub

    Private Sub window_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Me.DragMove()
    End Sub

    Private Sub CloseCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim errorText As String = ""
        If GlobalClass.GetMd5Hash(pbCurrentPassword.Password) <> _OldPassword Then
            errorText += ErrorMessages.[Default].CurrentPasswordNotCorrect + vbLf
        End If
        If pbNewPassword.Password <> pbConfirmNewPassword.Password Then
            errorText += ErrorMessages.[Default].ConflictPassword + vbLf
        End If
        If pbNewPassword.Password.Length < 3 Then
            errorText += ErrorMessages.[Default].PasswordEmpty + vbLf
        End If
        If errorText = "" Then
            Dim msgResult As MessageBoxResult = MessageBox.Show(ErrorMessages.[Default].ConfirmChangePassword, "", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)
            If msgResult = MessageBoxResult.Yes Then
                If objDatabaseClass.CheckConnection() Then
                    Dim newPass As String = GlobalClass.GetMd5Hash(pbNewPassword.Password)
                    Dim spName As String = "UPDATE_USER_PASS"
                    Dim spParams As Object() = New Object(1) {}
                    spParams(0) = _UserID
                    spParams(1) = newPass
                    Try
                        If objSqlDatabase.ExecuteNonQuery(spName, spParams) > 0 Then
                            GlobalClass._Password = newPass
                            MessageBox.Show(String.Format(ErrorMessages.[Default].ActionSucceed, "Change Password"))
                            Me.DialogResult = True
                        End If
                    Catch
                        MessageBox.Show(ErrorMessages.[Default].UnhandledException, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                    End Try
                Else
                    MessageBox.Show(ErrorMessages.[Default].NoConnection, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                    Me.DialogResult = False
                End If
            End If
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
        End If
    End Sub
End Class
